package cn.edu.ctgu;

public class Date {
    // 31天的月份
    private static int[] monthOne = new int[]{1,3,5,7,8,10,12};
    // 30天的月份
    private static int[] monthTwo = new int[]{4,6,9,11};
    private int year;
    private int month;
    private int day;
    public String getNextDate(String dateStr){
        String updateResult = this.updateDate(dateStr);
        if (updateResult.equals("success")){
            String checkResult = this.checkDate();
            if (checkResult.equals("valid")){
                return this.calcNextDate();
            }
            return checkResult;
        }
        return updateResult;
    }

    private String updateDate(String dateStr){
        // 获取年月日
        String[] numbers = dateStr.split("/");
        try{
            this.year = Integer.parseInt(numbers[0]);
        }catch (NumberFormatException e){
            return "year非法";
        }
        try{
            this.month = Integer.parseInt(numbers[1]);
        }catch (NumberFormatException e){
            return "month非法";
        }
        try{
            this.day = Integer.parseInt(numbers[2]);
        }catch (NumberFormatException e){
            return "day非法";
        }
        return "success";
    }

    // 检查日期是否合法
    private String checkDate(){
        String valid = "valid";
        String yearInvalid = "year非法";
        String monthInvalid = "month非法";
        String dayInvalid = "day非法";
        if (year>=1900&&year<=2050){
            if (month>=1&&month<=12){
                if (day<=0){
                    return dayInvalid;
                }
                if (month==2){
                    // 闰年
                    if (yearIsLeap(year)){
                        // 1-29
                        if (day<=29){
                            return valid;
                        }else{
                            return dayInvalid;
                        }
                    }
                    // 平年2月
                    else{
                        // 1-28
                        if (day<=28){
                            return valid;
                        }else{
                            return dayInvalid;
                        }
                    }
                }
                for(int i=0;i<7;++i){
                    if (month==monthOne[i]){
                        // 1-31
                        if (day<=31){
                            return valid;
                        }else{
                            return dayInvalid;
                        }
                    }
                }
                for(int i=0;i<4;++i){
                    if (month==monthTwo[i]){
                        // 1-30
                        if (day<=30){
                            return valid;
                        }else{
                            return dayInvalid;
                        }
                    }
                }
            }
            else{
                return monthInvalid;
            }
        }
        return yearInvalid;
    }

    private String calcNextDate(){
        int yearNext;
        int monthNext;
        int dayNext=day+1;
        int dayCarry=0;
        int monthCarry=0;
        if (month==2){
            // 闰年
            if (yearIsLeap(year)){
                // 1-29
                if (day==29){
                    dayNext = 1;
                    dayCarry = 1;
                }
            }
            else{
                // 1-28
                if (day==28){
                    dayNext = 1;
                    dayCarry = 1;
                }
            }
        }
        // 不是2月
        else{
            boolean isThirtyOne= false;
            // 是否为31天的月
            for(int i=0;i<7;++i){
                if (month==monthOne[i]){
                    isThirtyOne = true;
                    // 1-31
                    if (day==31){
                        dayNext = 1;
                        dayCarry = 1;
                    }
                    break;
                }
            }
            if (!isThirtyOne){
                // 1-30
                if (day==30){
                    dayNext = 1;
                    dayCarry = 1;
                }
            }
        }
        if (month+dayCarry>12){
            monthNext = 1;
            monthCarry = 1;
        }else{
            monthNext = month+dayCarry;
        }

        //  处理年
        yearNext = year+monthCarry;

        return yearNext +"/"+ monthNext +"/"+ dayNext;
    }

    // 判断某一年是否为闰年
    private boolean yearIsLeap(int year){
        if ((year%4==0&&year%100!=0)||(year%400==0)){
            return true;
        }
        return false;
    }
}

